package com.mt.fpb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.mt.fpb.common.util.DateUtil;
import com.mt.fpb.mapper.*;
import com.mt.fpb.model.TMineExploitation;
import com.mt.fpb.model.TMineInfo;
import com.mt.fpb.model.TVehicleOut;
import com.mt.fpb.model.TWarning;
import com.mt.fpb.model.dto.BaseQueryParams;
import com.mt.fpb.model.vo.CommonResult;
import io.swagger.models.auth.In;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import static com.mt.fpb.common.util.DateUtil.getPastDate;

/**
 * 矿山智慧监管平台 LT
 * <p>
 * 2021年6月13日11:41:48
 */
@RestController
@RequestMapping("/MineWisdom")
public class MineWisdomController {

    @Resource
    private TWarningMapper tWarningMapper;
    @Resource
    private TMineExploitationMapper tMineExploitationMapper;
    @Resource
    private TVehicleOutMapper tVehicleOutMapper;
    @Resource
    private TMineInfoMapper tMineInfoMapper;
    @Resource
    private TAirMonitorRecordMapper tAirMonitorRecordMapper;

    /**
     * 汇总信息
     * 左上
     * 2021年6月13日11:45:25
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getCollectInfo")
    public CommonResult getCollectInfo(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        JSONObject json = new JSONObject();
        //今日运作矿产
        List<Integer> list = tMineInfoMapper.TodaySwork(DateUtil.front(), DateUtil.getCurrentDate("yyyy-MM-dd HH"), queryParams.getMineId());
        json.put("operationDays", list.size());
        //今日噪音指数
        Integer noise = tAirMonitorRecordMapper.getAvgNoise(DateUtil.getCurrentDate("yyyy-MM-dd"), queryParams.getMineId());
        json.put("noise", noise);
        //总计矿场
        example.selectProperties("warningId");
        Integer count = tWarningMapper.selectCountByExample(example);
        json.put("count", count);
        example.clear();
        //总计异常于违规
        example.createCriteria().andEqualTo("warningType", 1).
                andEqualTo("warningType", 2).
                andEqualTo("warningType", 6).
                andEqualTo("warningType", 7);
        json.put("abnormalViolations", tWarningMapper.selectByExample(example).size());
        example.clear();
        //今日异常违规
        example.createCriteria().andEqualTo("warningType", 1).
                andEqualTo("warningType", 2).
                andEqualTo("warningType", 6).
                andEqualTo("warningType", 7).andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%");
        json.put("abnormalViolationsDays", tWarningMapper.selectByExample(example).size());
        example.clear();
        //今日TSP超标数
        example.createCriteria().andEqualTo("warningType", 1).andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%");
        json.put("TSPExceedingDays", tWarningMapper.selectByExample(example).size());
        example.clear();
        //总计处理告警
        example.createCriteria().andEqualTo("warningStatus", 1);
        json.put("alarmHandling", tWarningMapper.selectByExample(example).size());
        example.clear();
        //今日告警处理
        example.createCriteria().andLike("updateTime", "%" + DateUtil.getCurrentDate("yyyy-MM-dd") + "%").andEqualTo("warningStatus", 1);
        json.put("alarmHandlingDays", tWarningMapper.selectByExample(example).size());
        example.clear();
        return CommonResult.success(json);
    }

    /**
     * 越界开采预警
     * 左中
     * 2021年6月13日11:46:59
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getCrossBorderMining")
    public CommonResult getCrossBorderMining(BaseQueryParams queryParams) {
        Example example = new Example(TMineExploitation.class);
        JSONObject json = new JSONObject();
        //可开采总量
        example.createCriteria().andEqualTo("mineId", queryParams.getMineId());
        example.setCountProperty("total");
        json.put("recoverableCount", tMineExploitationMapper.selectCountByExample(example));
        example.clear();
        //已开采总量
        example.createCriteria().andEqualTo("mineId", queryParams.getMineId());
        example.setCountProperty("alreadyExploitation");
        json.put("alreadyExploitation", tMineExploitationMapper.selectCountByExample(example));
        example.clear();
        //预警事件总计
        example = new Example(TWarning.class);
        example.createCriteria().andEqualTo("mineId", queryParams.getMineId());
        example.selectProperties("warningId");
        json.put("earlyWarningCount", tWarningMapper.selectCountByExample(example));
        example.clear();
        //已处理事件
        example.createCriteria().andEqualTo("mineId", queryParams.getMineId()).
                andEqualTo("warningStatus", 1);
        example.selectProperties("warningId");
        json.put("dispose", tWarningMapper.selectCountByExample(example));
        example.clear();
        return CommonResult.success(json);
    }

    /**
     * 治超系统监控 LT
     * 左下
     * 2021年6月13日12:00:01
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getMonitoringInfo")
    public CommonResult getMonitoringInfo(BaseQueryParams queryParams) {
        //查询
        List<TWarning> vehicleOutDangerous = tVehicleOutMapper.getVehicleOutDangerous(queryParams.getMineId());
        return CommonResult.success(vehicleOutDangerous);
    }

    /**
     * 生产安全 LT
     * 右上
     * 2021年6月13日12:01:20
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getProductionSafety")
    public CommonResult getProductionSafety(BaseQueryParams queryParams) {
        JSONObject json = new JSONObject();
        //查询全年违规条数
        Integer count = tWarningMapper.getProductionSafety(DateUtil.getCurrentDate("yyyy"), queryParams.getMineId());
        if (StringUtils.isEmpty(count)) {
            count = 0;
        }
        //全年减去违规记录
        int result = 365 - count;
        //符合标准
        json.put("standard", result);
        //违规
        json.put("violation", count);
        return CommonResult.success(json);
    }

    /**
     * 生产违规记录
     * 右中
     * 2021年6月13日12:27:36
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getMiningViolations")
    public CommonResult getMiningViolations(BaseQueryParams queryParams) {
        Example example = new Example(TWarning.class);
        //生产违规记录
        example.createCriteria().orEqualTo("warningType", 2).
                orEqualTo("warningType", 14).
                andEqualTo("mineId", queryParams.getMineId());
        PageHelper.startPage(1, 10);
        List<TWarning> tWarnings = tWarningMapper.selectByExample(example);
        return CommonResult.success(tWarnings);
    }

    /**
     * 近7日环测数据 LT
     * 中下
     * 2021年6月13日15:15:17
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getRingMeasurementData")
    public CommonResult getRingMeasurementData(BaseQueryParams queryParams) {
        //获取最近7日时间
        List<String> list = DateUtil.findDates(DateUtil.convert2Date(getPastDate(6, "yyyy-MM-dd"), "yyyy-MM-dd"), DateUtil.convert2Date(DateUtil.getCurrentDate("yyyy-MM-DD"), "yyyy-MM-DD"));
        //TSP集合
        List<Integer> TSPList = new LinkedList<>();
        //PM2.5集合
        List<Integer> PMList = new LinkedList<>();
        //PM10
        List<Integer> PMLList = new LinkedList<>();
        //时间集合
        List<String> dateList = new LinkedList<>();

        //遍历
        for (String str : list) {
            //获取TSP平均值
            Integer TSP = tMineExploitationMapper.getTSP(str, queryParams.getMineId());
            TSPList.add(TSP);
            //获取PM2.5平均值
            Integer PM25 = tMineExploitationMapper.getPM25(str, queryParams.getMineId());
            PMList.add(PM25);
            //获取PM10
            Integer PM10 = tMineExploitationMapper.getPM10(str, queryParams.getMineId());
            PMLList.add(PM10);
            //转换成前端需要的时间格式
            dateList.add(DateUtil.convert2String(DateUtil.convert2Date(str, "yyyy-MM-dd"), "MM/dd"));
        }
        JSONObject json = new JSONObject();
        json.put("TSP", TSPList);
        json.put("PM25", PMList);
        json.put("PM10", PMLList);
        json.put("time", dateList);
        return CommonResult.success(json);
    }

    /**
     * 获取所有矿区数据 LT
     * 2021年6月16日15:54:40
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getListAll")
    public CommonResult getListAll(BaseQueryParams queryParams) {
        Example example = new Example(TMineInfo.class);
        example.createCriteria().andEqualTo("mineId", queryParams.getMineId());
        List<TMineInfo> tMineInfo = tMineInfoMapper.selectByExample(example);
        return CommonResult.success(tMineInfo);
    }

}
